package com.yikaiye.android.yikaiye.ui.activity.mine.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.application.MyApplication;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.MessageRecordsModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageListViewModel extends BaseViewModel {
    public MutableLiveData<List<MessageRecordsModel.MessageModel>> messageListMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> readSuccessMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> delSuccessMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> delErrorMutableLiveData = new MutableLiveData<>();

    /**
     * 消息列表
     */
    public void getMessageList(int page) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("pageSize", Constant.PAGE_SIZE);
        map.put("userId", MyApplication.manager.getUserId());
        addSubscribe(mRetrofitHelper.getMessageList(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<MessageRecordsModel>() {

            @Override
            public void onSuccess(BaseModel<MessageRecordsModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        messageListMutableLiveData.setValue(baseModel.getData().getRecords());
                    }
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }
        }));
    }

    /**
     * 全部标为已读
     */
    public void allReadMessage() {
        addSubscribe(mRetrofitHelper.allReady(MyApplication.manager.getUserId()).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    readSuccessMutableLiveData.setValue("success");
                }
            }
        }));
    }

    /**
     * 删除消息
     */
    public void deleteMessage(String messageId) {
        addSubscribe(mRetrofitHelper.deleteMessage(messageId).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    delSuccessMutableLiveData.setValue("success");
                } else {
                    delErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }
        }));
    }
}
